﻿using System.Diagnostics;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Windows.Automation;

//int currentProcessId = Process.GetProcessesByName("RoomsHost").FirstOrDefault().Id;
namespace UIAutomation
{
    internal class Program
    {
        private static void Main()
        {
            int currentProcessId = Process.GetProcessesByName("RoomsHost").FirstOrDefault().Id;
            AutomationElement root = AutomationElement.RootElement;

            Condition condition = new PropertyCondition(AutomationElement.ProcessIdProperty, currentProcessId);
            AutomationElementCollection windows = root.FindAll(TreeScope.Children, condition);

            var rootElements = new List<UIElementInfo>();

            foreach (AutomationElement window in windows)
            {
                var rootInfo = CollectElementInfo(window, 0);
                rootElements.Add(rootInfo);
                Traverse(window, rootInfo, 1);  // Start traversing children
            }

            var options = new JsonSerializerOptions
            {
                WriteIndented = false, // 去除空格和换行符
                NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals
            };

            string json = JsonSerializer.Serialize(rootElements, options);
            Console.WriteLine(json);
        }

        private static void Traverse(AutomationElement parent, UIElementInfo parentInfo, int indent)
        {
            TreeWalker walker = TreeWalker.ControlViewWalker;
            AutomationElement child = walker.GetFirstChild(parent);

            while (child != null)
            {
                var childInfo = CollectElementInfo(child, indent);
                parentInfo.Child.Add(childInfo);

                Traverse(child, childInfo, indent + 1);

                child = walker.GetNextSibling(child);
            }
        }

        private static UIElementInfo CollectElementInfo(AutomationElement e, int indent)
        {
            var c = e.Current;
            var r = c.BoundingRectangle;

            // 如果 BoundingRectangle 是无穷大，改为 0 或某个合理的值
            if (double.IsInfinity(r.X) || double.IsInfinity(r.Y) || double.IsInfinity(r.Width) || double.IsInfinity(r.Height))
            {
                r = new System.Windows.Rect(0, 0, 0, 0);  // 替换为合理的矩形
            }

            // 格式化 Bounds 为字符串，保留两位小数
            string boundsString = $"X={r.X:F2},Y={r.Y:F2},Width={r.Width:F2},Height={r.Height:F2}";

            return new UIElementInfo
            {
                Indent = indent,
                Type = c.ControlType.ProgrammaticName,
                Name = c.Name,
                AutomationId = c.AutomationId,
                ClassName = c.ClassName,
                FrameworkId = c.FrameworkId,
                LocalizedControlType = c.LocalizedControlType,
                IsEnabled = c.IsEnabled,
                IsKeyboardFocusable = c.IsKeyboardFocusable,
                HasKeyboardFocus = c.HasKeyboardFocus,
                IsOffscreen = c.IsOffscreen,
                Bounds = boundsString,  // 直接存储为字符串
                Child = new List<UIElementInfo>()  // Initialize child list
            };
        }
    }

    public class UIElementInfo
    {
        public int Indent { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
        public string AutomationId { get; set; }
        public string ClassName { get; set; }
        public string FrameworkId { get; set; }
        public string LocalizedControlType { get; set; }
        public bool IsEnabled { get; set; }
        public bool IsKeyboardFocusable { get; set; }
        public bool HasKeyboardFocus { get; set; }
        public bool IsOffscreen { get; set; }
        public string Bounds { get; set; }  // Now stored as a formatted string
        public List<UIElementInfo> Child { get; set; }  // Recursive child list
    }
}